package com.das.mechanic_base.adapter.alone;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.alone.AloneImageAndRecordBean;
import com.das.mechanic_base.def.alone.AloneUploadStatus;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.das.mechanic_base.widget.X3PointDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 初检车辆损伤
 */
public class X3FirstAloneRichCameraAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<AloneImageAndRecordBean> imgList;

    private Context mContext;

    private boolean isCamera;

    private int titleIndex;

    private final X3PointDialog delDialog;
    private int num;

    public X3FirstAloneRichCameraAdapter(Context mContext, boolean isCamera, int titleIndex, int num) {
        this.imgList = new ArrayList<>();
        this.mContext = mContext;
        this.isCamera = isCamera;
        this.titleIndex = titleIndex;
        this.num = num;
        delDialog = new X3PointDialog((Activity) mContext,"",mContext.getString(R.string.x3_affirm_delete_image),mContext.getString(R.string.x3_cancel_info), mContext.getString(R.string.x3_affirm_info));
    }

    public void changeData(List<AloneImageAndRecordBean> imageAndRecordBeans) {
        imgList.clear();
        imgList.addAll(imageAndRecordBeans);
        notifyDataSetChanged();
    }
    public List<AloneImageAndRecordBean> getImageList(){
        return imgList;
    }

    public void addData(AloneImageAndRecordBean imageAndRecordBeans) {
        imgList.add(imageAndRecordBeans);
        notifyItemChanged(imgList.size());
    }

    /**
     * 上传完图片需要判断上传状态
     * @param imageAndRecordBeans
     * @param imageIndex
     */
    public void changeData(AloneImageAndRecordBean imageAndRecordBeans,int imageIndex) {
        String path = imageAndRecordBeans.getPath();
        if (X3StringUtils.isEmpty(path)) {
            return;
        }
        for (int i = 0; i < imgList.size(); i++) {
            if (path.equals(imgList.get(i).getPath())) {
                imgList.get(i).setId(imageAndRecordBeans.getId());
                imgList.get(i).setUploadStatus(imageAndRecordBeans.getUploadStatus());
                notifyItemChanged(i);
                break;
            }
        }

    }

    /**
     * 删除某一张图片
     * @param imageIndex
     */
    public void deleteImage(int imageIndex) {
        if (imageIndex >= imgList.size()) {
            return;
        }
        imgList.remove(imageIndex);
        notifyItemRemoved(imageIndex);
        notifyItemRangeChanged(imageIndex,imgList.size() + 1 - imageIndex);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new CameraNewHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_first_alone_rich_camera_item,parent,false));
        }
        return new CameraClickHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_first_alone_rich_camera_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == 0) {
            // 照片模块
            CameraNewHolder newHolder = (CameraNewHolder) holder;

            newHolder.iv_image.setImageAlpha(255);

            newHolder.iv_image.setTransitionName("share");

            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) newHolder.iv_image.getLayoutParams();
            int width = X3ScreenUtils.getScreenWidth(mContext);
            int itemH = (width - X3ScreenUtils.dipToPx(84,mContext)) / 3;
            Log.e("itemH_","itemH = " + itemH );
            lp.height = itemH;
            lp.width = itemH;
            newHolder.iv_image.setLayoutParams(lp);

            String imagePath = imgList.get(position).getPath();

            /**
             * 上传状态
             */
            long uploadStatus = imgList.get(position).getUploadStatus();

            switch ((int) uploadStatus) {
                case 0:
                    newHolder.image_delete.setVisibility(View.GONE);
                    newHolder.iv_bg.setVisibility(View.VISIBLE);
                    newHolder.pb_bar.setVisibility(View.VISIBLE);
                    newHolder.iv_failed.setVisibility(View.GONE);
                    newHolder.iv_bg.setOnClickListener(null);
                    break;
                case 1:
                    newHolder.image_delete.setVisibility(View.VISIBLE);
                    newHolder.iv_bg.setVisibility(View.GONE);
                    newHolder.pb_bar.setVisibility(View.GONE);
                    newHolder.iv_failed.setVisibility(View.GONE);
                    newHolder.iv_bg.setOnClickListener(null);
                    break;
                case 2:
                    newHolder.image_delete.setVisibility(View.VISIBLE);
                    newHolder.iv_bg.setVisibility(View.VISIBLE);
                    newHolder.pb_bar.setVisibility(View.GONE);
                    newHolder.iv_failed.setVisibility(View.VISIBLE);
                    newHolder.iv_bg.setOnClickListener(v -> {
                        // 重新上传图片
                        String httpPath = imgList.get(position).getHttpPath();
                        if (!X3StringUtils.isEmpty(httpPath) && !httpPath.contains("storage")) {
                            // 网络图片不为空 无需上传
                            imgList.get(position).setUploadStatus(AloneUploadStatus.UPLOADING_SUCCESS);
                            notifyItemChanged(position);

                            if (iOnItemCameraClick != null) {
                                iOnItemCameraClick.iOnDeleteImage(titleIndex,imgList);
                            }
                            return;
                        }
                        String path = imgList.get(position).getPath();
                        if (X3StringUtils.isEmpty(path)) {
                            // 本地图片为空 无需上传 并且需要删除
                            imgList.remove(position);//集合移除该条
                            notifyItemRemoved(position);//通知移除该条
                            notifyItemRangeChanged(position,imgList.size() + 1 - position);
                            /**
                             * 并且在数据库中要删除
                             */
                            if (iOnItemCameraClick != null) {
                                iOnItemCameraClick.iOnDeleteImage(titleIndex,imgList);
                            }
                            return;
                        }
                        File file = new File(path);
                        if (!file.exists()) {
                            // 本地无法找到此图片 同样要从列表中移除

                            imgList.remove(position);
                            notifyItemRemoved(position);
                            notifyItemRangeChanged(position,imgList.size() + 1 - position);
                            /**
                             * 并且在数据库中要删除
                             */
                            if (iOnItemCameraClick != null) {
                                iOnItemCameraClick.iOnDeleteImage(titleIndex,imgList);
                            }
                            return;
                        }
                        imgList.get(position).setUploadStatus(AloneUploadStatus.UPLODING);
                        notifyItemChanged(position);
                        if (iOnItemCameraClick != null) {
                            iOnItemCameraClick.iOnAgainUploadImage(imgList.get(position).getPath(),titleIndex,position);
                        }
                    });
                    break;
            }
            if(!X3StringUtils.isEmpty(imagePath)){
                if (imagePath.endsWith(".mp4")) {
                    newHolder.iv_play.setVisibility(View.VISIBLE);
                } else {
                    newHolder.iv_play.setVisibility(View.GONE);
                }
            }
            if (!X3StringUtils.isEmpty(imagePath)) {
                newHolder.iv_play.setVisibility(imagePath.endsWith(".mp4") ? View.VISIBLE : View.GONE);

                if (!imagePath.contains("storage") && !imagePath.contains("http://")) {
                    imagePath = "http://" + imagePath;
                }
                //Glide.with(mContext).load(imagePath).apply(myOptions).into(newHolder.iv_image);
                X3GlideNewUtils.loadRoundCornerImage(mContext,imagePath,((CameraNewHolder) holder).iv_image, X3ScreenUtils.dipToPx(4,mContext),R.mipmap.x3_car_img_bg);
            }

            newHolder.iv_image.setClickable(false);
            newHolder.iv_image.setOnClickListener(v -> {

                List<String> imageList = new ArrayList<>();
                for (AloneImageAndRecordBean imageBean : imgList) {
                    imageList.add(imageBean.getPath());
                }
                X3Utils.startShowImage(mContext,imageList,position);
            });
            newHolder.image_delete.setOnClickListener(view -> {

                if (delDialog != null) {
                    delDialog.show();
                }
                delDialog.setiBtnClick(new X3PointDialog.IBtnClick() {
                    @Override
                    public void iBtnAffirmClick() {
                        if (imgList.size() > 0 && position < imgList.size()) {
                                imgList.remove(position);
                                notifyItemRemoved(position);
                                notifyItemRangeChanged(position,imgList.size() + 1 - position);
                                /**
                                 * 并且在数据库中要删除
                                 */
                                if (iOnItemCameraClick != null) {
                                    iOnItemCameraClick.iOnDeleteImage(titleIndex,imgList);
                                }
                                return;
                        }
                    }

                    @Override
                    public void iBtnCancelClick() {
                    }
                });
            });

        }else {
            CameraClickHolder clickHolder = (CameraClickHolder) holder;
            clickHolder.iv_image.setImageAlpha(255);
            // 拍照模块
            clickHolder.iv_image.setImageResource(R.mipmap.x3_camera);

            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) clickHolder.iv_image.getLayoutParams();
            int h = X3ScreenUtils.getScreenWidth(mContext);
            int itemH = (h - X3ScreenUtils.dipToPx(84,mContext)) / 3;
            Log.e("itemH_","itemH = " + itemH );
            lp.height = itemH;
            lp.width = itemH;
            clickHolder.iv_image.setLayoutParams(lp);

            clickHolder.image_delete.setVisibility(View.GONE);
            clickHolder.iv_play.setVisibility(View.GONE);
            if (isCamera) { // 可以拍照
                if(num > imgList.size()){
                    clickHolder.iv_image.setVisibility(View.VISIBLE);
                }else {
                    clickHolder.iv_image.setVisibility(View.GONE);
                }
                clickHolder.iv_image.setOnClickListener(v -> {

                    if (iOnItemCameraClick != null) {
                        iOnItemCameraClick.iOnItemCameraClick(position);
                    }
                });
            } else {
                // 不能拍照
                clickHolder.iv_image.setVisibility(View.GONE);
            }
        }

    }

    public List<AloneImageAndRecordBean> getCameraList () {
        return imgList;
    }

    IOnItemCameraClick iOnItemCameraClick;

    public void setiOnItemCameraClick(IOnItemCameraClick iOnItemCameraClick) {
        this.iOnItemCameraClick = iOnItemCameraClick;
    }

    public interface IOnItemCameraClick {

        void iOnItemCameraClick(int imageIndex);

        /**
         * 删除回调
         */
        void iOnDeleteClick(long resId,int position,int titleIndex,String path);

        /**
         * 点击重新上传图片
         * @param path
         * @param titleIndex
         * @param imageIndex
         */
        void iOnAgainUploadImage(String path,int titleIndex,int imageIndex);

        /**
         * 之前没有上传成功 则直接删除
         * @param titleIndex
         */
        void iOnDeleteImage(int titleIndex,List<AloneImageAndRecordBean> mList);
    }

    @Override
    public int getItemCount() {
        return imgList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < imgList.size()) {
            return 0;
        }
        return 1;
    }

    class CameraNewHolder extends RecyclerView.ViewHolder {
        ImageView iv_image;
        ImageView image_delete;
        ImageView iv_play;
        ImageView iv_failed;
        ImageView iv_bg;
        ProgressBar pb_bar;
        public CameraNewHolder(@NonNull View itemView) {
            super(itemView);
            iv_image = itemView.findViewById(R.id.iv_image);
            image_delete = itemView.findViewById(R.id.image_delete);
            iv_play = itemView.findViewById(R.id.iv_play);
            iv_failed = itemView.findViewById(R.id.iv_failed);
            iv_bg = itemView.findViewById(R.id.iv_bg);
            pb_bar = itemView.findViewById(R.id.pb_bar);
        }
    }

    class CameraClickHolder extends RecyclerView.ViewHolder {
        ImageView iv_image;
        ImageView image_delete;
        ImageView iv_play;
        public CameraClickHolder(@NonNull View itemView) {
            super(itemView);
            iv_image = itemView.findViewById(R.id.iv_image);
            image_delete = itemView.findViewById(R.id.image_delete);
            iv_play = itemView.findViewById(R.id.iv_play);
        }
    }
}
